package com.sjgtw.cloud.system.controller.service;

import com.alibaba.fastjson.JSONObject;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.system.api.domain.SysDept;
import com.sjgtw.cloud.system.api.domain.SysUser;
import com.sjgtw.cloud.system.service.ISysDeptService;
import com.sjgtw.cloud.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/service/")
public class SystemServiceController {

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysUserService userService;

    @GetMapping(value = "/getDeptById")
    public AjaxResult getDeptById(Long deptId)
    {
        return AjaxResult.success(deptService.selectDeptById(deptId));
    }

    @GetMapping(value = "/getUserByNames")
    public AjaxResult getUserByNames(String names)
    {
        return AjaxResult.success(userService.getUserByNames(names));
    }

    @GetMapping(value = "/getUserByIds")
    public AjaxResult getUserByIds(String userIds)
    {
        return AjaxResult.success(userService.getUserByIds(userIds));
    }


    @GetMapping(value = "/getSubDeptIdsByUserId")
    public AjaxResult getSubDeptIdsByUserId(Long userId)
    {
        SysUser sysUser = userService.selectUserById(userId);
        if (sysUser != null) {
            List<SysDept> sysDepts = deptService.selectNormalChildrenDeptListById(sysUser.getDeptId());
            List<Long> deptIds = sysDepts.stream().map(SysDept::getDeptId).collect(Collectors.toList());
            deptIds.add(sysUser.getDeptId());
            return AjaxResult.success(deptIds);
        }
        return AjaxResult.success();
    }

    @GetMapping(value = "/getDeptByNames")
    public AjaxResult getDeptByNames(String deptNames)
    {
        List<SysDept> depts = deptService.selectByNames(deptNames);
        return AjaxResult.success(depts);
    }

    @GetMapping(value = "/getUserDeptLevel")
    public AjaxResult getUserDeptLevel(Long userId)
    {
        SysUser sysUser = userService.selectUserById(userId);
        Long deptId = sysUser.getDeptId();
        SysDept sysDept = deptService.selectDeptById(deptId);
        JSONObject jsonObject = new JSONObject();

        if(sysDept.getAncestors()!=null){
            String[] split = sysDept.getAncestors().split(",");

            if(split.length == 2){
                jsonObject.put("dept","section");
                jsonObject.put("parentDept",sysDept.getParentId());
                jsonObject.put("deptId",sysDept.getDeptId());
            }else if(split.length == 3){
                jsonObject.put("dept","workArea");
                jsonObject.put("parentId",sysDept.getParentId());
                jsonObject.put("deptId",sysDept.getDeptId());
            }
        }

        return AjaxResult.success(jsonObject);
    }


}
